package com.uphone.kingmall.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.china.zhuoyue.R;
import com.uphone.kingmall.bean.CatsListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tzh on 2020/6/15.
 */
public class ShopGoodsCatsAdapter extends BaseAdapter {
    private List<CatsListBean.CatsBean> dataList;
    private LayoutInflater inflater;
    public int selectItem;

    public ShopGoodsCatsAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        dataList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public CatsListBean.CatsBean getItem(int position) {
        if (dataList.size() == 0 || dataList.size() < position) {
            return null;
        }
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.kingmall_adapter_shop_goods_cats, parent, false);
            holderView = new HolderView(convertView);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView.getTag();
        }
        if (dataList.size() > position) {
            CatsListBean.CatsBean bean = dataList.get(position);
            holderView.tv_cat.setText(bean.getCatName());
            if (bean.shopCarGoodsCatNum > 0) {
                holderView.tv_goods_count.setText(String.valueOf(bean.shopCarGoodsCatNum));
                holderView.tv_goods_count.setVisibility(View.VISIBLE);
            } else {
                holderView.tv_goods_count.setVisibility(View.GONE);
            }
        }
        if (selectItem == position) {
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));
            holderView.v_tag.setVisibility(View.VISIBLE);
        } else {
            convertView.setBackgroundColor(Color.parseColor("#00000000"));
            holderView.v_tag.setVisibility(View.GONE);
        }
        return convertView;
    }

    public void setData(List<CatsListBean.CatsBean> cats) {
        if (cats == null || cats.size() == 0) return;
        dataList = cats;
    }

    private final static class HolderView {
        TextView tv_cat, tv_goods_count;
        View v_tag;

        HolderView(View view) {
            tv_cat = view.findViewById(R.id.tv_cat);
            tv_goods_count = view.findViewById(R.id.tv_goods_count);
            v_tag = view.findViewById(R.id.v_tag);
        }
    }
}
